package com.hewep.support.print;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.print.Book;
import java.awt.print.PageFormat;
import java.awt.print.Paper;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import java.util.List;

import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.attribute.HashAttributeSet;
import javax.print.attribute.standard.PrinterName;
import javax.swing.ImageIcon;

import com.jfinal.kit.StrKit;

public class Printer implements Printable {

	/**
	 * @param Graphic指明打印的图形环境 
	 * PageFormat指明打印页格式（页面大小以点为计量单位，1点为1英寸的1/72，1英寸为25.4毫米。A4纸大致为595×842点） 
	 * pageIndex指明页号
	 **/
	int offsetX;
	int offsetY;
	int width;
	int height;
	int pageNumbers = 1;
	String content;
	String imgPath;
	List<String> imgPaths;
	String[] nums;
	
	public Printer(String content) {
		this.content = content;
	}
	public Printer(String content, int offsetX, int offsetY, int width, int height ) {
		this.content = content;
		this.offsetX = offsetX;
		this.offsetY = offsetY;
		this.width = (int)(width*2.833);
		this.height = (int)(height*2.833);
	}
	
	public void setPageNum(int PageNumbers) {
		this.pageNumbers = PageNumbers;
	}

	public int getPageNum() {
		return pageNumbers;
	}


	public int print(Graphics grap, PageFormat pf, int pageIndex)
			throws PrinterException {
		Graphics2D g2 = (Graphics2D) grap;
		// 设置打印颜色为黑色
		g2.setColor(Color.black);
		//Java平台所定义的五种字体系列：Serif、SansSerif、Monospaced、Dialog 和 DialogInput
		Font font = new Font("SansSerif", Font.PLAIN, 10); 
		g2.setFont(font);
		
		int k = font.getSize();
		if (pageIndex >= pageNumbers)
			return NO_SUCH_PAGE;
		
	       
		double ascent = 13;
		double y = ascent;
		if(StrKit.notBlank(content)){
			String[] contents = content.split("\n");
			for (int i = 0; i < contents.length; i++) {
				g2.drawString(contents[i], (float) 15, (float) ascent+(k*(i+1))); // 第二排
			}
			y += (k*(contents.length+1));
		}
		
		return PAGE_EXISTS;
	}
	
	public void printContent(String PrintQ) {
		// 通俗理解就是书、文档
		Book book = new Book();
		// 设置成竖打
		PageFormat pf = new PageFormat();
		pf.setOrientation(PageFormat.PORTRAIT); // LANDSCAPE表示竖打;PORTRAIT表示横打;REVERSE_LANDSCAPE表示打印空白
		// 通过Paper设置页面的空白边距和可打印区域。必须与实际打印纸张大小相符。
		Paper p = new Paper();
		p.setSize(width, height);
		p.setImageableArea(offsetX, offsetY, width, height); // Print Area
		pf.setPaper(p);
		// 把 PageFormat 和 Printable 添加到书中，组成一个页面
		book.append(this, pf, this.getPageNum());
		String printerName = PrintQ;
		HashAttributeSet hs = new HashAttributeSet();
		hs.add(new PrinterName(printerName, null));
		// 获取打印服务对象
		PrintService[] printService = PrintServiceLookup.lookupPrintServices(
				null, hs);
		PrinterJob job = PrinterJob.getPrinterJob();
		try {
			if (printService.length > 0) {
				job.setPrintService(printService[0]);
			}
			// 设置打印类
			job.setPageable(book);
			// Print
			job.print();

		} catch (PrinterException e) {
			e.printStackTrace();
		}
	}
	
	
	public int getOffsetX() {
		return offsetX;
	}

	public void setOffsetX(int offsetX) {
		this.offsetX = offsetX;
	}

	public int getOffsetY() {
		return offsetY;
	}

	public void setOffsetY(int offsetY) {
		this.offsetY = offsetY;
	}

	public int getWidth() {
		return width;
	}

	public void setWidth(int width) {
		this.width = width;
	}

	public int getHeight() {
		return height;
	}

	public void setHeight(int height) {
		this.height = height;
	}
	
}
